import {createRouter, createWebHashHistory} from "vue-router";
import routes from './routers'
import Antd from 'ant-design-vue';
import {message} from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
import {createApp} from 'vue'
import App from './App';
import * as Icons from '@ant-design/icons-vue';
import mitt from "mitt";

const routers = createRouter({
    history: createWebHashHistory(),
    routes,
})

//beforeEach每次进行路由跳转时都会执行
routers.beforeEach((to, from, next) => {
    const token = localStorage.getItem("token");
    if (to.matched.length === 0) {　　　　　　//未匹配到的时候跳转到404页面
        message.warning("未找到页面: " + to.path);
        return;
    } else if (to.path === '/toLogin' || to.path === '/register' || to.path === '/retrievePassword') {
        next();
    } else if (token) {
        next();
    } else {
        message.warning("您还没有登录，请先登录");
        next('/toLogin');
    }
})

let app = createApp(App);
app.config.globalProperties.$bus = new mitt()
app.use(Antd);
app.config.productionTip = false;
app.use(routers);
app.mount(`#app`);
// 循环使用全部全部图标
const icons = Icons;
for (const i in icons) {
    // 全局注册一下组件
    app.component(i, icons[i])
}
